using Microsoft.EntityFrameworkCore;
using PandaWebAPI.Models.ef;

namespace PandaWebAPI.Services;

/// <summary>
/// 站点统计服务实现
/// </summary>
public class SiteStatisticService : ISiteStatisticService
{
    private readonly PandaSystemContext _context;

    public SiteStatisticService(PandaSystemContext context)
    {
        _context = context;
    }

    /// <inheritdoc />
    public async Task<IEnumerable<SiteStatistic>> GetAllAsync()
    {
        return await _context.SiteStatistics
            .OrderByDescending(s => s.StatDate)
            .ToListAsync();
    }

    /// <inheritdoc />
    public async Task<SiteStatistic?> GetByIdAsync(Guid id)
    {
        return await _context.SiteStatistics.FindAsync(id);
    }

    /// <inheritdoc />
    public async Task<SiteStatistic?> GetByDateAsync(DateOnly date)
    {
        return await _context.SiteStatistics
            .FirstOrDefaultAsync(s => s.StatDate == date);
    }

    /// <inheritdoc />
    public async Task<IEnumerable<SiteStatistic>> GetByDateRangeAsync(DateOnly startDate, DateOnly endDate)
    {
        return await _context.SiteStatistics
            .Where(s => s.StatDate >= startDate && s.StatDate <= endDate)
            .OrderBy(s => s.StatDate)
            .ToListAsync();
    }

    /// <inheritdoc />
    public async Task<SiteStatistic> CreateAsync(SiteStatistic siteStatistic)
    {
        siteStatistic.StatId = Guid.NewGuid();
        _context.SiteStatistics.Add(siteStatistic);
        await _context.SaveChangesAsync();
        return siteStatistic;
    }

    /// <inheritdoc />
    public async Task<SiteStatistic> UpdateAsync(SiteStatistic siteStatistic)
    {
        _context.Entry(siteStatistic).State = EntityState.Modified;
        await _context.SaveChangesAsync();
        return siteStatistic;
    }

    /// <inheritdoc />
    public async Task DeleteAsync(Guid id)
    {
        var siteStatistic = await _context.SiteStatistics.FindAsync(id);
        if (siteStatistic != null)
        {
            _context.SiteStatistics.Remove(siteStatistic);
            await _context.SaveChangesAsync();
        }
    }

    /// <inheritdoc />
    public async Task<SiteStatistic> UpdateTodayStatsAsync()
    {
        var today = DateOnly.FromDateTime(DateTime.UtcNow);
        var stats = await _context.SiteStatistics
            .FirstOrDefaultAsync(s => s.StatDate == today);

        if (stats == null)
        {
            stats = new SiteStatistic
            {
                StatDate = today,
                UserCount = await _context.Users.CountAsync(),
                PostCount = await _context.Posts.CountAsync(p => p.IsDeleted != true),
                CommentCount = await _context.Comments.CountAsync(),
                OrderCount = await _context.Orders.CountAsync(),
                PageViews = 0
            };
            _context.SiteStatistics.Add(stats);
        }
        else
        {
            stats.UserCount = await _context.Users.CountAsync();
            stats.PostCount = await _context.Posts.CountAsync(p => p.IsDeleted != true);
            stats.CommentCount = await _context.Comments.CountAsync();
            stats.OrderCount = await _context.Orders.CountAsync();
        }

        await _context.SaveChangesAsync();
        return stats;
    }
} 